/***********
 * js 截断
 * @example:  <a href='xxx' limit="2">xxxx</a> 会将xxxx截断成xx
 ***********/
function cut_str(str,lim, tip){
	var pos = 0;
	var len = 0;
	var limited = '';
	while(pos < str.length && len < lim){
		limited += str.charAt(pos);
		pos		+= 1;
		len		+= (str.charCodeAt(pos)>255)?2:1;
	}
	return limited + ( (pos < str.length)?tip:'');
}
$(document).ready(function(){
	$('a[limit],span[limit]').each(function(n,i){
		var tag = $(i);
		var tip = typeof(tag.attr('tip'))=='undefined' ? '...' : tag.attr('tip');
		tag.text(cut_str(tag.text(), tag.attr('limit'), tip));
	});
	$('textarea').keyup(function(){
        var counter=$(this).parent().find('.max-count');
        if (counter.length){
            var limit=parseInt(counter.attr('max'));
            current_count = Math.floor(limit-$(this).val().replace(/[^\x00-\xff]/ig,"xx").length/2);///[\u4e00-\u9fa5]/g
            counter.text(current_count.toString());
        };
    });
    $('textarea').change(function(){ $('textarea').keyup(); });
    $('textarea').keyup();
});
/***********
 * 提示气泡
 * @example:  
 ***********/
function note_info(msg,obj,type){
	var sel = '#'+$(obj).attr('id');
	var color = type=='warn'?'#F58323':'#A47160';
	var frame = layer.tips(msg, sel, {
	    tips: [1, color] //还可配置颜色
	})
	setTimeout(function(){
		var index = layer.getFrameIndex(frame);
		layer.close(index);
	},1500);
}

function getScrollTop(){
    var scrollTop=0;
    if(document.documentElement && document.documentElement.scrollTop){
        scrollTop=document.documentElement.scrollTop;
    }
    else if(document.body){
        scrollTop=document.body.scrollTop;
    }
    return scrollTop;
}

function myAjax(url,data,title,fun){
	if(data.indexOf("=")<1){data = $("#"+data).serialize();}
	$.ajax({
		type: "POST",
		url:url+'.php',
		data:data,//$('#accorder').serialize(),
		async: false,
		dataType:'json',
		success: function(result) {
			
			if(typeof(fun)=='function'){
				var ok = 1
				fun(result);
			}else{
				var ok = 2
				if(result=='suc')note_info(title,'success',ev);
				else			 note_info(result,'warn'  ,ev);
			}
			
		}
	});
}